package cn.rongdao.jrkabao.ui.cashcoupon.uicomponent;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mifengkong.frtools.util.FRCollection;
import com.mifengkong.frtools.util.FRString;

import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.bean.cashcoupon.GetCashCouponBean;
import cn.rongdao.jrkabao.bean.cashcoupon.SelectCashCouponBean;
import cn.rongdao.jrkabao.ui.cashadvance.CashAdvanceActivity;

/**
 * Created by jack on 2017/8/15
 */

public class CashCouponUIComponent extends LinearLayout {

    public CashCouponUIComponent(Context context) {
        super(context);
    }

    public CashCouponUIComponent(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public CashCouponUIComponent(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private LinearLayout llMoney;
    private LinearLayout llRestDay;
    private TextView tvMoney;
    private TextView tvMoneyCondition;
    private ImageView ivTag;
    private TextView tvRestDay;
    private TextView tvName;
    private TextView tvTime;
    private TextView tvMoneyDescription;
    private ImageView ivSelect;

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        llMoney = findViewById(R.id.icc_ll_money);
        llRestDay = findViewById(R.id.icc_ll_rest_day);
        tvMoney = findViewById(R.id.icc_tv_money);
        tvMoneyCondition = findViewById(R.id.icc_tv_money_condition);
        ivTag = findViewById(R.id.icc_iv_tag);
        tvRestDay = findViewById(R.id.icc_tv_rest_day);
        TextView tvImmediate = findViewById(R.id.icc_tv_immediate);
        tvName = findViewById(R.id.icc_tv_name);
        tvTime = findViewById(R.id.icc_tv_time);
        tvMoneyDescription = findViewById(R.id.icc_tv_money_description);
        ivSelect = findViewById(R.id.icc_iv_select);

        tvImmediate.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                CashAdvanceActivity.start(getContext(), "");
            }
        });
    }

    public void setContent(boolean isUnUse, boolean isInValid, GetCashCouponBean data) {
        setVisibility(View.VISIBLE);
        if (isUnUse) {   //未使用
            setUnUseContentStyle();
            llRestDay.setVisibility(View.VISIBLE);
            if (data.getLeft_day() <= 5) {
                tvRestDay.setVisibility(View.VISIBLE);
                if (data.getLeft_day() <= 1) {
                    tvRestDay.setText(FRCollection.appendAsString("最后一天"));
                } else {
                    tvRestDay.setText(FRCollection.appendAsString("剩", FRString.valueOf(data.getLeft_day()), "天", ""));
                }
            }
        } else {
            setOtherContentStyle();
            ivTag.setVisibility(View.VISIBLE);
            if (!isInValid) {
                ivTag.setBackgroundResource(R.drawable.icon_tag_use);
            } else {
                ivTag.setBackgroundResource(R.drawable.icon_tag_invalid);
            }
        }
        tvMoney.setText(getMoneyStyle(data.getValue()));
        tvMoneyCondition.setText(FRCollection.appendAsString("满", FRString.valueOf(data.getCondition()), "可用", ""));
        tvName.setText(FRString.valueOf(data.getCoupon_name()));
        tvTime.setText(FRCollection.appendAsString("有效期至：", FRString.valueOf(data.getOver_time()), ""));
        tvMoneyDescription.setText(FRString.valueOf(data.getCoupon_desc()));
    }

    public void setContent(SelectCashCouponBean.ListBean data) {
        setVisibility(View.VISIBLE);
        if (data.getIs_valid() == 1) {
            ivSelect.setVisibility(View.VISIBLE);
            setUnUseContentStyle();
            ivSelect.setSelected(data.isSelect());
        } else {
            setOtherContentStyle();
        }
        tvMoney.setText(getMoneyStyle(data.getAmount()));
        tvMoneyCondition.setText(FRCollection.appendAsString("满", FRString.valueOf(data.getCondition()), "可用", ""));
        tvName.setText(FRString.valueOf(data.getCoupon_name()));
        tvTime.setText(FRCollection.appendAsString("有效期至：", FRString.valueOf(data.getValid_time()), ""));
        tvMoneyDescription.setText(FRString.valueOf(data.getCoupon_desc()));
    }

    private SpannableString getMoneyStyle(String money) {
        money = FRCollection.appendAsString("¥", money, "");
        SpannableString ss = new SpannableString(money);
        ss.setSpan(new AbsoluteSizeSpan(32, true), 1, money.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        return ss;
    }

    private void setUnUseContentStyle() {
        llMoney.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.colorPrimary));
        setTextColor(R.color.c333333);
    }

    private void setOtherContentStyle() {
        llMoney.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.ccccccc));
        setTextColor(R.color.ccccccc);
    }

    private void setTextColor(int colorRes) {
        tvName.setTextColor(ContextCompat.getColor(getContext(), colorRes));
        tvTime.setTextColor(ContextCompat.getColor(getContext(), colorRes));
        tvMoneyDescription.setTextColor(ContextCompat.getColor(getContext(), colorRes));
    }
}
